package com.ygnn.service.impl;

import com.ygnn.common.Dto.OrderDto;
import com.ygnn.common.Enum.ExceptionEnum;
import com.ygnn.common.Vo.OrderVo;
import com.ygnn.common.context.BaseContext;
import com.ygnn.exception.BizException;
import com.ygnn.mapper.OrderMapper;
import com.ygnn.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * ClassName:OrderServiceImpl
 * Package:com.ygnn.service.impl
 * Description:
 *
 * @Author: dlh
 * @Creat:2024/10/8-22:20
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    // 定义日期格式
    private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public void insert(OrderDto orderDto,int status) throws ParseException {
        if(orderDto.getUserId()<10000000||orderDto.getUserId()>99999999)
        {
            throw new BizException(ExceptionEnum.USER_ID_ERROR);
        }
        //处理数据：
        orderDto.setStatus(status);
        String openid = BaseContext.getCurrentId();
        orderDto.setOpenid(openid);
        orderDto.setAmount(orderDto.getAmount());//TODO 金额先写死
        //设置结束时间
        Date startDate = orderDto.getStartDate();
        int kms = orderDto.getKm();
        int times = orderDto.getTimes();
        orderDto.setEndDate(SDF.parse(getEndDate(startDate,kms,times)));
        long orderNumber = System.currentTimeMillis() / 1000;
        orderDto.setOrderId(orderNumber);
        orderMapper.add(orderDto);
        log.info("新增订单:{}",orderDto);
    }

    @Override
    public List<OrderVo> selectAll() {
        log.info("查询所有订单ing");
        return orderMapper.selectAll();
    }

    @Override
    public List<OrderVo> selectByDate(Date date) {

        return orderMapper.selectByDate(date);
    }

    @Override
    public List<OrderVo> selectById(int id) {
        if(id<10000000||id>99999999)
        {
            throw new BizException(ExceptionEnum.USER_ID_ERROR);
        }
        log.info("根据查询id:",id);
        return orderMapper.selectById(id);
    }

    @Override
    public void upDateById(int id,int status,String msg) {
        orderMapper.UpDateById(id,status,msg);
    }

    @Override
    public List<OrderVo> selectByOpenid(String openid) {
        return orderMapper.selectByOpenid(openid);
    }


    public static String getEndDate(Date startDate, int kms, int times) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        if (kms==3){
            // 获取开始日期的星期数
            int startDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

            // 计算开始日期的下一天的星期数
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            int nextDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

            calendar.setTime(startDate); // 将日历重新设置为开始日期

            boolean flag=false;
            // 开始遍历日期
            while (times > 0) {
                if (flag){
                    // 先递增一天
                    calendar.add(Calendar.DAY_OF_MONTH, 1);
                }
                flag=true;

                // 获取当前日期的星期数
                int currentDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

                // 如果当前日期的星期数等于开始日期的星期数或者下一天的星期数，则减去times
                if (currentDayOfWeek == startDayOfWeek || currentDayOfWeek == nextDayOfWeek) {
                    times--;
                }
            }

            return SDF.format(calendar.getTime());
        }else if(kms==1){
            boolean flag=false;
            // 开始遍历日期
            while (times > 0) {
                if (flag){
                    // 先递增一天
                    calendar.add(Calendar.DAY_OF_MONTH, 1);
                }
                flag=true;
                // 获取当前日期的星期几
                int currentDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
                // 如果当前日期不是周六(7)也不是周日(1)，则减去times
                if (currentDayOfWeek != Calendar.SATURDAY && currentDayOfWeek != Calendar.SUNDAY) {
                    times--;
                }
            }
            return SDF.format(calendar.getTime());
        }else {
            return null;
        }

    }
}
